﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestPlatform.Core;
using Aquiles.Helpers.Encoders;
using Apache.Cassandra;
using Aquiles.Core.Cluster;
using Aquiles.Cassandra10;

namespace TestPlatform.Performer.Cassandra
{
    public class CassandraGetByIdPerformer : BaseGetByIdPerformer
    {
        public override string UnderlyinEngine { get { return "Cassandra"; } }

        public CassandraGetByIdPerformer()
            : base(new CassandraHelper())
        {

        }

        protected override void EntityGetById(Guid id)
        {
            byte[] key = ByteEncoderHelper.GuidEncoder.ToByteArray(id);

            ColumnPath loginPath = new ColumnPath()
            {
                Column = ByteEncoderHelper.UTF8Encoder.ToByteArray("login"),
                Column_family = CassandraHelper.UserCF,
            };

            ColumnPath namePath = new ColumnPath()
            {
                Column = ByteEncoderHelper.UTF8Encoder.ToByteArray("name"),
                Column_family = CassandraHelper.UserCF,
            };

            ICluster cluster = AquilesHelper.RetrieveCluster("MGR-Cluster");

            object login = cluster.Execute(new ExecutionBlock(c =>
            {
                return c.get(key, loginPath, ConsistencyLevel.ONE);
            }), "MGR");

            object name = cluster.Execute(new ExecutionBlock(c =>
            {
                return c.get(key, namePath, ConsistencyLevel.ONE);
            }), "MGR");



            Defer(id,
                ByteEncoderHelper.UTF8Encoder.FromByteArray(((ColumnOrSuperColumn)login).Column.Value),
                ByteEncoderHelper.UTF8Encoder.FromByteArray(((ColumnOrSuperColumn)name).Column.Value));         
        }
    }
}
